#include "picturereceiver.h"
#include "qevent.h"
#include "line.h"
#include "widget.h"

#include <QFileDialog>
#include <QPainter>
#include <vector>
#include <queue>
#include <QInputDialog>
#include <QMessageBox>

const int MAXN=250;
std::vector<line> l;
std::vector<QPoint> point;//动态数组记录点
int pointIterator = 0;// 记录总共的点数
int lineIterator = 0;// 记录总共的边数
int stateInsult=0;// 用于储存checkboxInsult的状态
int stateClear=0;//用于储存checkboxClear的状态
int stateConnect=0;//用于储存checkboxConnect的状态
int stateClearLine=0;//用于储存checkboxDelete
int stateDelete=0;//用于直线删除
int stateLine=0;//用于完成直线连接
int stateShowWeight=0;//用于储存checkboxShow的状态
int stateSetWeight=0;//用于储存checkBoxSet的状态
int stateChangeMode=0;// 用于储存combobox的状态
int state=0;//用于储存事件mousepressEvent中的状态参数
int start,final;//用于储存起点和终点
int stateChooseStart=0;//用于储存是否选择好起点和终点
int stateSetStart=0;//用于记录checkboxsetstart的状态
int stateDrawThePath=0;
int stateHighlight[2]={-1,-1};
extern void setUnable();
extern void refreshState();
extern void setMode();
extern void showResult(QString s);
extern Widget* pw;
int*ptr;//指向最短路径顺序的指针
int ptrLength=0;//记录该指针指向数组的长度
float lenchange[2]={2.5,-2.5};

PictureReceiver::PictureReceiver(QWidget *parent) : QWidget{parent}
{}

struct node {
    int dis, u;

    bool operator>(const node& a) const { return dis > a.dis; }
};

bool PictureReceiver::spfa(int start,int f)//这是单源模式下的最短路径
{
    int pre[MAXN];//记录到达某点所需最短距离路径的前驱点
    memset(pre,-1,sizeof(pre));
    int e[MAXN][MAXN];
    for(int i = 0;i<pointIterator;i++){
        memset(e[i], 0x3f, (pointIterator + 1) * sizeof(int));
    }
    for(auto ed:l){
        e[ed.start][ed.final]=ed.distance;
    }
    int dis[MAXN], cnt[MAXN], vis[MAXN];
    std::queue<int> q;
    memset(dis, 0x3f, (pointIterator + 1) * sizeof(int));
    memset(cnt, 0, sizeof(cnt));
    memset(vis, 0, sizeof(vis));
    dis[start] = 0;
    vis[start] = 1;
    pre[start] = start;
    q.push(start);
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        vis[u] = 0;
        for (int i=0;i<pointIterator;i++) {
            int final = i, distance = e[u][i];
            if (dis[final] > dis[u] + distance) {
                dis[final] = dis[u] + distance;
                cnt[final] = cnt[u] + 1;// 记录最短路经过的边数
                pre[final]=u;
                if (cnt[final] >= pointIterator) {
                    return false;
                }
                // 在不经过负环的情况下，最短路至多经过 n - 1 条边
                // 因此如果经过了多于 n 条边，一定说明经过了负环
                if (!vis[final]){
                    q.push(final);
                    vis[final] = 1;
                }
            }
        }
    }
    if(pre[f]==-1){
        QString str="no path!";
        pw->showResult(str);
    }else{
        findThepath(pre,start,f);
        QString str=createThePath(ptr,ptrLength);
        pw->showResult(str);
        stateDrawThePath=1;
        update();
        stateDrawThePath=0;
    }
    return true;
}

bool PictureReceiver::Johnson()
{
    int martix[MAXN][MAXN];//邻接矩阵
    memset(martix,-1,sizeof(martix));
    int e[MAXN][MAXN];
    for(int i = 0;i<pointIterator;i++){
        memset(e[i], 0x3f, (pointIterator + 1) * sizeof(int));
    }
    memset(e[pointIterator],0,sizeof(e[pointIterator]));
    for(auto ed:l){
        e[ed.start][ed.final]=ed.distance;
    }
    //spfa判断是否有负权环
    int dis[MAXN], cnt[MAXN], vis[MAXN];
    std::queue<int> q;
    memset(dis, 0x3f, (pointIterator + 1) * sizeof(int));
    memset(cnt, 0, sizeof(cnt));
    memset(vis, 0, sizeof(vis));
    int start = pointIterator;
    dis[start] = 0;
    vis[start] = 1;
    q.push(start);
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        vis[u] = 0;
        for (int i = 0;i<pointIterator;i++) {
            int final = i, distance = e[u][i];//  可能会因为无穷有问题
            if (dis[final] > dis[u] + distance) {
                dis[final] = dis[u] + distance;
                cnt[final] = cnt[u] + 1;  // 记录最短路经过的边数
                if (cnt[final] >= pointIterator){
                    return false;
                }
                // 在不经过负环的情况下，最短路至多经过 n - 1 条边
                // 因此如果经过了多于 n 条边，一定说明经过了负环
                if (!vis[final]){
                    q.push(final);
                    vis[final] = 1;
                }
            }
        }
    }
    // 改变权值化负为正
    for(auto ed:l){
        e[ed.start][ed.final]+=dis[ed.start]-dis[ed.final];
    }
    //逐点运行dijkstra算法（二叉堆优化）
    memset(dis, 0x3f, (pointIterator + 1) * sizeof(int));
    memset(vis, 0, sizeof(vis));
    std::priority_queue<node, std::vector<node>, std::greater<node>> p;
    for(int s = 0;s<pointIterator;s++){
        martix[s][s]=s;
        dis[s] = 0;
        p.push({0, s});
        while (!p.empty()) {
            qDebug()<<1;
            int u = p.top().u;
            p.pop();
            if (vis[u]) {
                continue;
            }
            vis[u] = 1;
            for (int i=0;i<pointIterator;i++) {
                int final =i , w = e[u][i];
                if (dis[final] > dis[u] + w) {
                    dis[final] = dis[u] + w;
                    p.push({dis[final], final});
                    martix[s][final]=u;
                }
            }
        }
        while (!p.empty()) {
            p.pop();
        }
        memset(dis, 0x3f, (pointIterator + 1) * sizeof(int));
        memset(vis, 0, sizeof(vis));
    }
    for(int i = 0;i<pointIterator;i++){
        QString s="";
        for(int j=0;j<pointIterator;j++){
            if(martix[i][j]>=0){
                s+=" " + QString::number(martix[i][j])+" ";
            }else{
                s+=QString::number(martix[i][j])+" ";
            }
        }
        pw->showResult(s);
    }
    return true;
}

void PictureReceiver::findThepath(int pre[], int s, int f)//倒序储存
{
    ptr=new int[pointIterator];
    int i =0;
    int cur=f;
    while(cur!=s){
        ptr[i++]=cur;
        cur=pre[cur];
    }
    ptr[i++]=s;
    ptrLength=i;
}

QString PictureReceiver::createThePath(int ptr[],int length)
{
    QString str="";
    for(int i = length-1;i>=0;i--){
        if(i!=0){
            str+=(QString::number(ptr[i])+"->");
        }else{
            str+=(QString::number(ptr[i]));
        }
    }
    return str;
}


void PictureReceiver::insultPicture()//插入图片
{
    if(fileName.isEmpty()){
        fileName = QFileDialog::getOpenFileName(this, tr("Open Image"), "/home/jana", tr("Image Files (*.png *.jpg *.bmp)"));
        pic.load(fileName);
        update();
        QMessageBox::warning(this, "警告", "在您完成窗口大小调节并绘点后，请勿随意拉动边框，否则将导致点的位置脱离图片中的位置！");
    }
    else{//若未删除原图则弹窗
        QMessageBox::warning(this, "警告", "尚未删除原图片");
    }
}

void PictureReceiver::clearPicture()//清除图片
{
    pic.load("");
    std::vector<QPoint>().swap(point);//注意这里需要优化：clear（）并没有真正清除vector
    std::vector<line>().swap(l);//注意这里需要优化：clear（）并没有真正清除vector
    pointIterator=0;
    lineIterator=0;
    update();
    fileName.clear();
}

void PictureReceiver::insultPoint()//checkbox“插入点”
{
    if(stateInsult){
        stateInsult = 0;
        pw->refreshState();
    }
    else{
        stateInsult = 1;
        pw->setUnable();
    }//模拟点击勾选框后状态改变
    if(stateInsult){
        state = 1;//此时可以在pictureReceiver插入点
    }
    else{
        state = 0;
    }
}

void PictureReceiver::clearPoint()//checkbox"删除点“  //注意此处和上面的install有矛盾，应当设置当一个完成勾选时另一个不能点击
{
    if(stateClear){
        stateClear = 0;
        pw->refreshState();
    }
    else{
        stateClear = 1;
        pw->setUnable();
    }//模拟点击勾选框后状态改变
    if(stateClear){
        state = 2;//此时可以删除点
    }
    else{
        state = 0;
    }
}

void PictureReceiver::connectLine()//连接直线
{
    if(stateConnect){
        stateConnect = 0;
        pw->refreshState();
    }
    else{
        stateConnect = 1;
        pw->setUnable();
    }
    if(stateConnect){
        state = 3;
    }else{
        state = 0;
    }
}

void PictureReceiver::clearLine()//清除直线
{
    if(stateClearLine){
        stateClearLine = 0;
        pw->refreshState();
    }
    else{
        stateClearLine = 1;
        pw->setUnable();
    }
    if(stateClearLine){
        state = 4;
    }else{
        state = 0;
    }
}

void PictureReceiver::showWeight()//展示权重
{
    if(stateShowWeight){
        stateShowWeight = 0;
    }
    else{
        stateShowWeight = 1;
    }
    if(stateShowWeight){
        stateSWP = 1;
        update();
    }else{
        stateSWP = 0;
        update();
    }
}

void PictureReceiver::setWeight()//自定义权重
{
    if(stateSetWeight){
        stateSetWeight = 0;
        pw->refreshState();
    }
    else{
        stateSetWeight = 1;
        pw->setUnable();
    }
    if(stateSetWeight){
        state = 5;
    }else{
        state = 0;
    }
}

void PictureReceiver::setStartAndFinal()//选定起点和终点
{
    if(stateSetStart){
        stateSetStart=0;
        pw->refreshState();
    }
    else{
        stateSetStart=1;
        pw->setUnable();
    }
    if(stateSetStart){
        state=6;
    }else{
        state=0;
    }
}

void PictureReceiver::showShortestPath()//显示最短路径
{
    if(stateChangeMode){
        if(Johnson()){
        }
        else{
            QString s = "This graph has a negative ring!";
            pw->showResult(s);
        }
    }else{
        if(stateChooseStart){
            if(spfa(start,final)){

            }else{
                QString s = "This graph has a negative ring!";
                pw->showResult(s);
            }
        }else{
            QMessageBox::warning(this, "警告", "请先选择起点和终点");//这里添加弹窗提醒不存在直线
        }
    }
}



void PictureReceiver::changeMode()//选定为单源or全源模式
{
    if(stateChangeMode){
        stateChangeMode = 0;//单源为0，全源为1
        pw->setMode();
    }
    else{
        stateChangeMode = 1;
        pw->setMode();
    }
}

void PictureReceiver::paintEvent(QPaintEvent *event)//重写绘图事件
{
    QPainter painter(this);
    painter.drawPixmap(rect(),pic);
    pw->setMinimumSize(100, 100);
    pw->setMaximumSize(2500,2500);
    QFont font;
    font.setPointSize(7); // 设置字体大小为 20 磅
    painter.setFont(font);
    painter.setRenderHint(QPainter::Antialiasing, true);
    for(int i = 1;i<=pointIterator;i++){
        if(stateHighlight[0]==i-1||stateHighlight[1]==i-1){
            QPen pen = QPen(QColor(0, 255, 0), 1, Qt::SolidLine);
            painter.setPen(pen);
        }else{
            QPen pen = QPen(QColor(0, 0, 0), 1, Qt::SolidLine);
            painter.setPen(pen);
        }
        painter.drawEllipse(point[i-1], 10, 10);//把点画出来
        QString str=QString::number(i-1);
        if(i-1<10){
            painter.drawText(QPoint(point[i-1].x()-3,point[i-1].y()+3),str);//添加点上的文字标签
        }else{
            painter.drawText(QPoint(point[i-1].x()-5,point[i-1].y()+3),str);//添加点上的文字标签
        }//保证打印出的恰好在中间
    }
    for(int i = 1;i<=lineIterator;i++){//画直线
        if(l[i-1].distance!=INFINITY){
            qreal arrowSize = 5;
            int flag=0;
            for(int k =ptrLength-1;k>0;k--){
                if(l[i-1].isLine(ptr[k],ptr[k-1])){
                    flag=1;
                    break;
                }
            }
            if(flag){
                QPen pen = QPen(QColor(0, 0, 255), 1, Qt::SolidLine);
                painter.setPen(pen);
                painter.setBrush(pen.color());
            }
            else{
                QPen pen = QPen(QColor(0, 0, 0), 1, Qt::SolidLine);
                painter.setPen(pen);
                painter.setBrush(pen.color());
            }
            if(l[i-1].start<l[i-1].final){
                if((point[l[i-1].start].x()-point[l[i-1].final].x())*(point[l[i-1].start].y()-point[l[i-1].final].y())>0||point[l[i-1].start].x()-point[l[i-1].final].x()==0){
                    QPointF S(point[l[i-1].start].x()+4,point[l[i-1].start].y()-4);
                    QPointF F(point[l[i-1].final].x()+4,point[l[i-1].final].y()-4);
                    QLineF line(F,S);
                    double angle = std::atan2(-line.dy(), line.dx());
                    QPointF arrowP1 = line.p1() + QPointF(sin(angle + M_PI / 3) * arrowSize,
                                                          cos(angle + M_PI / 3) * arrowSize);
                    QPointF arrowP2 = line.p1() + QPointF(sin(angle + M_PI - M_PI / 3) * arrowSize,
                                                          cos(angle + M_PI - M_PI / 3) * arrowSize);
                    QPolygonF arrowHead;
                    arrowHead.clear();
                    arrowHead << line.p1() << arrowP1 << arrowP2;
                    painter.drawLine(line);
                    painter.drawPolygon(arrowHead);//绘制带箭头的直线
                }else{
                    QPointF S(point[l[i-1].start].x()-4,point[l[i-1].start].y()-4);
                    QPointF F(point[l[i-1].final].x()-4,point[l[i-1].final].y()-4);
                    QLineF line(F,S);
                    double angle = std::atan2(-line.dy(), line.dx());
                    QPointF arrowP1 = line.p1() + QPointF(sin(angle + M_PI / 3) * arrowSize,
                                                          cos(angle + M_PI / 3) * arrowSize);
                    QPointF arrowP2 = line.p1() + QPointF(sin(angle + M_PI - M_PI / 3) * arrowSize,
                                                          cos(angle + M_PI - M_PI / 3) * arrowSize);
                    QPolygonF arrowHead;
                    arrowHead.clear();
                    arrowHead << line.p1() << arrowP1 << arrowP2;
                    painter.drawLine(line);
                    painter.drawPolygon(arrowHead);//绘制带箭头的直线
                }
            }else{
                if((point[l[i-1].start].x()-point[l[i-1].final].x())*(point[l[i-1].start].y()-point[l[i-1].final].y())>0||point[l[i-1].start].x()-point[l[i-1].final].x()==0){
                    QPointF S(point[l[i-1].start].x()-4,point[l[i-1].start].y()+4);
                    QPointF F(point[l[i-1].final].x()-4,point[l[i-1].final].y()+4);
                    QLineF line(F,S);
                    double angle = std::atan2(-line.dy(), line.dx());
                    QPointF arrowP1 = line.p1() + QPointF(sin(angle + M_PI / 3) * arrowSize,
                                                          cos(angle + M_PI / 3) * arrowSize);
                    QPointF arrowP2 = line.p1() + QPointF(sin(angle + M_PI - M_PI / 3) * arrowSize,
                                                          cos(angle + M_PI - M_PI / 3) * arrowSize);
                    QPolygonF arrowHead;
                    arrowHead.clear();
                    arrowHead << line.p1() << arrowP1 << arrowP2;
                    painter.drawLine(line);
                    painter.drawPolygon(arrowHead);//绘制带箭头的直线
                }else{
                    QPointF S(point[l[i-1].start].x()+4,point[l[i-1].start].y()+4);
                    QPointF F(point[l[i-1].final].x()+4,point[l[i-1].final].y()+4);
                    QLineF line(F,S);
                    double angle = std::atan2(-line.dy(), line.dx());
                    QPointF arrowP1 = line.p1() + QPointF(sin(angle + M_PI / 3) * arrowSize,
                                                          cos(angle + M_PI / 3) * arrowSize);
                    QPointF arrowP2 = line.p1() + QPointF(sin(angle + M_PI - M_PI / 3) * arrowSize,
                                                          cos(angle + M_PI - M_PI / 3) * arrowSize);
                    QPolygonF arrowHead;
                    arrowHead.clear();
                    arrowHead << line.p1() << arrowP1 << arrowP2;
                    painter.drawLine(line);
                    painter.drawPolygon(arrowHead);//绘制带箭头的直线
                }
            }
            if(stateSWP){
                int flag=0;
                for(int k =ptrLength-1;k>0;k--){
                    if(l[i-1].isLine(ptr[k],ptr[k-1])){
                        flag=1;
                        break;
                    }
                }
                if(flag){
                    QPen pen = QPen(QColor(0, 0, 255), 2, Qt::SolidLine);
                    painter.setPen(pen);
                    painter.setBrush(pen.color());
                }
                else{
                    QPen pen = QPen(QColor(255, 0, 0), 2, Qt::SolidLine);
                    painter.setPen(pen);
                    painter.setBrush(pen.color());
                }
                if(l[i-1].start<l[i-1].final){
                    if((point[l[i-1].start].x()-point[l[i-1].final].x())*(point[l[i-1].start].y()-point[l[i-1].final].y())>0||point[l[i-1].start].x()-point[l[i-1].final].x()==0){
                        QPointF S(point[l[i-1].start].x()+4,point[l[i-1].start].y()-4);
                        QPointF F(point[l[i-1].final].x()+4,point[l[i-1].final].y()-4);
                        QPoint tmp((S.x()+F.x())/2+4,(S.y()+F.y())/2-4);
                        double angle = std::atan2(-(S.y()-F.y()), S.x()-F.x());
                        painter.translate(tmp.x(),tmp.y());
                        if(0<-angle/M_PI*180&&-angle/M_PI*180<90){
                            painter.rotate(-angle/M_PI*180);
                        }else if(-angle/M_PI*180>90){
                            painter.rotate(180-angle/M_PI*180);
                        }else if(-angle/M_PI*180>-90&&-angle/M_PI*180<0){
                            painter.rotate(-angle/M_PI*180);
                        }else{
                            painter.rotate(180-angle/M_PI*180);
                        }
                        painter.drawText(0,0,QString::number(l[i-1].distance) );
                        painter.resetTransform();
                        painter.translate(0, 0);//这里执行将打印的distance旋转
                    }else{
                        QPointF S(point[l[i-1].start].x()-4,point[l[i-1].start].y()-4);
                        QPointF F(point[l[i-1].final].x()-4,point[l[i-1].final].y()-4);
                        QPoint tmp((S.x()+F.x())/2-4,(S.y()+F.y())/2-4);
                        double angle = std::atan2(-(S.y()-F.y()), S.x()-F.x());
                        painter.translate(tmp.x(),tmp.y());
                        if(0<-angle/M_PI*180&&-angle/M_PI*180<90){
                            painter.rotate(-angle/M_PI*180);
                        }else if(-angle/M_PI*180>90){
                            painter.rotate(180-angle/M_PI*180);
                        }else if(-angle/M_PI*180>-90&&-angle/M_PI*180<0){
                            painter.rotate(-angle/M_PI*180);
                        }else{
                            painter.rotate(180-angle/M_PI*180);
                        }
                        painter.drawText(0,0,QString::number(l[i-1].distance) );
                        painter.resetTransform();
                        painter.translate(0, 0);//这里执行将打印的distance旋转
                    }
                }else{
                    if((point[l[i-1].start].x()-point[l[i-1].final].x())*(point[l[i-1].start].y()-point[l[i-1].final].y())>0||point[l[i-1].start].x()-point[l[i-1].final].x()==0){
                        QPointF S(point[l[i-1].start].x()-4,point[l[i-1].start].y()+4);
                        QPointF F(point[l[i-1].final].x()-4,point[l[i-1].final].y()+4);
                        QPoint tmp((S.x()+F.x())/2-4,(S.y()+F.y())/2+4);
                        double angle = std::atan2(-(S.y()-F.y()), S.x()-F.x());
                        painter.translate(tmp.x(),tmp.y());
                        if(0<-angle/M_PI*180&&-angle/M_PI*180<90){
                            painter.rotate(-angle/M_PI*180);
                        }else if(-angle/M_PI*180>90){
                            painter.rotate(180-angle/M_PI*180);
                        }else if(-angle/M_PI*180>-90&&-angle/M_PI*180<0){
                            painter.rotate(-angle/M_PI*180);
                        }else{
                            painter.rotate(180-angle/M_PI*180);
                        }
                        painter.drawText(-2,2,QString::number(l[i-1].distance) );
                        painter.resetTransform();
                        painter.translate(0, 0);//这里执行将打印的distance旋转
                    }else{
                        QPointF S(point[l[i-1].start].x()+4,point[l[i-1].start].y()+4);
                        QPointF F(point[l[i-1].final].x()+4,point[l[i-1].final].y()+4);
                        QPoint tmp((S.x()+F.x())/2+4,(S.y()+F.y())/2+4);
                        double angle = std::atan2(-(S.y()-F.y()), S.x()-F.x());
                        painter.translate(tmp.x(),tmp.y());
                        if(0<-angle/M_PI*180&&-angle/M_PI*180<90){
                            painter.rotate(-angle/M_PI*180);
                        }else if(-angle/M_PI*180>90){
                            painter.rotate(180-angle/M_PI*180);
                        }else if(-angle/M_PI*180>-90&&-angle/M_PI*180<0){
                            painter.rotate(-angle/M_PI*180);
                        }else{
                            painter.rotate(180-angle/M_PI*180);
                        }
                        painter.drawText(2,2,QString::number(l[i-1].distance) );
                        painter.resetTransform();
                        painter.translate(0, 0);//这里执行将打印的distance旋转
                    }
                }
            }
        }
    }
    if(ptrLength){
        ptrLength=0;
        delete[] ptr;//在这里进行清空，从而保证每次显示最短路径后自动清空
    }
}

void PictureReceiver::mousePressEvent(QMouseEvent *event)//重写鼠标点击事件
{
    if(state==1){//添加点
        QPoint temp = event->pos();
        int flag =1;
        for(int i = 1;i<=pointIterator;i++){
            if(temp.x()<point[i].x()+30&&temp.x()>point[i].x()-30&&temp.y()<point[i].y()+30&&temp.y()>point[i].y()-30){
                flag = 0;
                QMessageBox::warning(this, "警告", "点的距离过近");
                break;
            }
        }
        if(flag){
        point.push_back(temp);
        pointIterator++;
        update();
        }
    }
    if(state==2){//删除点
        int x0=event->pos().x();
        int y0=event->pos().y();
        for(int i =1;i<=pointIterator;i++){
            if(point[i-1].x()-10<x0&&x0<point[i-1].x()+10&&point[i-1].y()-10<y0&&point[i-1].y()+10>y0){
                point.erase(point.begin()+i-1);
                pointIterator--;
                int tempLineIterator=lineIterator;
                for(int j = tempLineIterator;j>=1;j--){
                    if(l[j-1].start==i-1||l[j-1].final==i-1){
                        l.erase(l.begin()+j-1);
                        lineIterator--;
                    }
                }
                for(int j =1;j<=lineIterator;j++){
                    if(l[j-1].start>i-1)l[j-1].start--;
                    if(l[j-1].final>i-1)l[j-1].final--;
                }
                update();
                return;
            }
        }
    }
    if(state==3){//判断是否重复连接
        //设置每次连两个，完成一次连线后自动重置
        int x0=event->pos().x();
        int y0=event->pos().y();
        for(int i =1;i<=pointIterator;i++){
            if(point[i-1].x()-10<x0&&x0<point[i-1].x()+10&&point[i-1].y()-10<y0&&point[i-1].y()+10>y0){
                line1[stateLine] = i-1;
                stateHighlight[stateLine]=i-1;
                update();
                if(stateLine<1){
                    stateLine++;
                }else{
                    stateLine=0;
                    for(int j =1;j<=lineIterator;j++){
                        if(l[j-1].isLine(line1[0],line1[1])){
                            QMessageBox::warning(this, "警告", "请勿重复连接");
                            stateHighlight[0]=-1;
                            stateHighlight[1]=-1;
                            update();
                            return;
                        }
                    }
                    if(line1[0]!=line1[1]){
                        QLineF a(point[line1[0]],point[line1[1]]);
                        float templength=a.length();
                        class line templ={line1[0],line1[1],templength};
                        l.push_back(templ);
                        lineIterator++;
                        update();
                    }else{
                        QMessageBox::warning(this, "警告", "请勿连接同一点");
                    }
                    line1[0]=0;
                    line1[1]=0;
                    stateHighlight[0]=-1;
                    stateHighlight[1]=-1;
                    update();
                }
                break;
            }
        }
    }
    if(state==4){//此处应该添加判断是否有直线的逻辑
        int x0=event->pos().x();
        int y0=event->pos().y();
        for(int i =1;i<=pointIterator;i++){
            if(point[i-1].x()-10<x0&&x0<point[i-1].x()+10&&point[i-1].y()-10<y0&&point[i-1].y()+10>y0){
                line2[stateDelete] = i-1;
                stateHighlight[stateDelete]=i-1;
                update();
                if(stateDelete<1){
                    stateDelete++;
                }
                else{
                    stateDelete=0;
                    int templ=-1;
                    for(int j = 1;j <= lineIterator;j++){
                        if(l[j-1].isLine(line2[0],line2[1])){
                            templ=j-1;
                            break;
                        }
                    }//查询所确定的line在l数组中的位置
                    if(templ!=-1){
                        l.erase(l.begin()+templ);
                        lineIterator--;
                        update();
                    }
                    else{

                        QMessageBox::warning(this, "警告", "该直线不存在");//这里添加弹窗提醒不存在直线
                    }
                    line2[0]=0;
                    line2[1]=0;
                    stateHighlight[0]=-1;
                    stateHighlight[1]=-1;
                    update();
                }
                break;
            }
        }
    }
    if(state==5){//此处应该添加判断是否有直线的逻辑
        int x0=event->pos().x();
        int y0=event->pos().y();
        for(int i =1;i<=pointIterator;i++){
            if(point[i-1].x()-10<x0&&x0<point[i-1].x()+10&&point[i-1].y()-10<y0&&point[i-1].y()+10>y0){
                line3[stateSetWeight-1] = i-1;
                stateHighlight[stateSetWeight-1]=i-1;
                update();
                if(stateSetWeight==1){
                    stateSetWeight++;
                }
                else{
                    stateSetWeight=1;
                    int templ=-1;
                    for(int i = 1;i <= lineIterator;i++){
                        if(l[i-1].isLine(line3[0],line3[1])){
                            templ=i-1;
                        }
                    }
                    if(templ!=-1){
                        line3[0]=0;
                        line3[1]=0;
                        double weight = QInputDialog::getDouble(this,"自定义权重","请输入你所想要的权重");
                        if(weight<100000000&&weight>-100000000){
                            l[templ].distance=weight;
                            update();
                        }else{
                            QMessageBox::warning(this, "警告", "请勿输入过大的数");
                        }
                    }else{
                        line3[0]=0;
                        line3[1]=0;
                        QMessageBox::warning(this, "警告", "该直线不存在");//这里添加弹窗提醒不存在直线
                    }
                    stateHighlight[0]=-1;
                    stateHighlight[1]=-1;
                    update();
                }
                break;
            }
        }
    }
    if(state==6){
        int x0=event->pos().x();
        int y0=event->pos().y();
        for(int i =1;i<=pointIterator;i++){
            if(point[i-1].x()-10<x0&&x0<point[i-1].x()+10&&point[i-1].y()-10<y0&&point[i-1].y()+10>y0){
                line4[stateSetStart-1] = i-1;
                stateHighlight[stateSetStart-1]=i-1;
                update();
                if(stateSetStart==1){
                    stateSetStart++;
                }
                else{
                    stateSetStart=1;
                    if(line4[0]!=line4[1]){
                        start=line4[0];
                        final=line4[1];
                        stateChooseStart=1;
                    }
                    else{
                        QMessageBox::warning(this, "警告", "不要选择同一点");//这里添加弹窗提醒
                    }
                    line4[0]=0;
                    line4[1]=0;
                    stateHighlight[0]=-1;
                    stateHighlight[1]=-1;
                    update();
                }
                break;
            }
        }
    }
}
